package cn.iocoder.yudao.module.bpm.controller.admin.sourcecatalog;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.sourcecatalog.vo.SourceCatalogPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.sourcecatalog.vo.SourceCatalogRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.sourcecatalog.vo.SourceCatalogSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.sourcecatalog.SourceCatalogDO;
import cn.iocoder.yudao.module.bpm.service.sourcecatalog.SourceCatalogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 资源编目")
@RestController
@RequestMapping("/bpm/source-catalog")
@Validated
public class SourceCatalogController {

    @Resource
    private SourceCatalogService sourceCatalogService;

    @PostMapping("/create")
    @Operation(summary = "创建资源编目")
    @PreAuthorize("@ss.hasPermission('dr:source-cata:create')")
    public CommonResult<Long> createSourceCatalog(@Valid @RequestBody SourceCatalogSaveReqVO createReqVO) {
        return success(sourceCatalogService.createSourceCatalog(getLoginUserId(),createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源编目")
    @PreAuthorize("@ss.hasPermission('dr:source-cata:update')")
    public CommonResult<Boolean> updateSourceCatalog(@Valid @RequestBody SourceCatalogSaveReqVO updateReqVO) {
        sourceCatalogService.updateSourceCatalog(getLoginUserId(),updateReqVO);
        return success(true);
    }

    @PutMapping("/reback")
    @Operation(summary = "更新资源编目")
    @PreAuthorize("@ss.hasPermission('dr:source-cata:update')")
    public CommonResult<Boolean> rebackSourceCatalog(@Valid @RequestBody SourceCatalogSaveReqVO updateReqVO) {
        sourceCatalogService.rebackSourceCatalog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源编目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:source-cata:delete')")
    public CommonResult<Boolean> deleteSourceCatalog(@RequestParam("id") Long id) {
        sourceCatalogService.deleteSourceCatalog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源编目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:source-cata:query')")
    public CommonResult<SourceCatalogRespVO> getSourceCatalog(@RequestParam("id") Long id) {
        SourceCatalogDO sourceCatalog = sourceCatalogService.getSourceCatalog(id);
        return success(BeanUtils.toBean(sourceCatalog, SourceCatalogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源编目分页")
    @PreAuthorize("@ss.hasPermission('dr:source-cata:query')")
    public CommonResult<PageResult<SourceCatalogRespVO>> getSourceCatalogPage(@Valid SourceCatalogPageReqVO pageReqVO) {
        PageResult<SourceCatalogDO> pageResult = sourceCatalogService.getSourceCatalogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SourceCatalogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源编目 Excel")
    @PreAuthorize("@ss.hasPermission('dr:source-cata:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSourceCatalogExcel(@Valid SourceCatalogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SourceCatalogDO> list = sourceCatalogService.getSourceCatalogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源编目.xls", "数据", SourceCatalogRespVO.class,
                        BeanUtils.toBean(list, SourceCatalogRespVO.class));
    }

}